<?php

/**
 * ArticleService.class.php 
 * 
 *
 * @author     late.xiao@qq.com
 */
namespace Sysblog\Service;

class ArticleService extends BaseService {
    protected $status = array(1=>'已发布', 0=>'草稿');
    
    public function getRow($id, $field='*')
    {
        if(!$id || !is_numeric($id))
        {
            throw new \Exception(L('_ERR_FORMART_AID_'));
        }
        
        $obj_mdl = D('article');
        $rs = $obj_mdl->where('aid=%d',intval($id))->field($field)->find();
        if(!$rs)
        {
            throw new \Exception(L('_ERR_NOT_HAS_'));
        }
        
        return $rs;
    }
    
    // 保存数据
    public function saveData($data)
    {
        $this->__checkData($data);
        $obj_mdl = D('article');
        
        if($data['aid'])
        {
            $data['updated'] = NOW_TIME;
            
            $where['aid'] = $data['aid'];
            unset($data['aid']);
            
            return $obj_mdl->where($where)->save($data);
        }
        
        $data['created'] = $data['updated'] = NOW_TIME;
        
        return $obj_mdl->data($data)->add();
    }
    
    
    // 删除一条数据
    public function delRow($aid)
    {
        $aid = intval($aid);
        if(!$aid)
        {
            return false;
        }
        $obj_mdl = D('article');
        $where['aid'] = $aid;
        return $obj_mdl->where($where)->delete();
    }
    public function getArticleList($where = array(), $field='*', $start=0, $limit=10, $order='')
    {
        $data = $this->getList($where,$field,$start,$limit,$order);
        if($data)
        {
            $this->__formatList($data['list']);
        }
        
        return $data;
    }
    public function getList($where = array(), $field='*', $start=0, $limit=10, $order='')
    {
        
        $count = $this->count($where);
        if(!$count)
        {
            return false;
        }
        $obj_mdl = D('article');
        
        $list = $obj_mdl->field($field)->where($where)->limit($start, $limit)->order($order)->select();
        
        if(!$list)
        {
            return false;
        }
        
        return compact('count','list');
    }
    
    public function count($where)
    {
        $obj_mdl = D('article');
        $count = $obj_mdl->where($where)->count();
        
        return $count;
    }
    
    private function __formatList(&$list)
    {
        if(!$list)
        {
            return false;
        }
        
        // 获取分类名称
        $cids = array_column($list, 'node_id');
        $obj_serv_cat = D('Category', 'Service');
        $cids = array_unique($cids);
        
        $catList = $obj_serv_cat->getCatList('node_id,node_name', array('node_id' => array('IN',$cids)));
        $catList = array_column($catList, 'node_name', 'node_id');
        foreach ($list as &$val)
        {
            $val['cname'] = !empty($catList[$val['node_id']]) ? $catList[$val['node_id']] : '未分类';
            $val['status'] = $this->status[$val['ifpub']];
        }
        
        return true;
    }
    
    private function __checkData($data)
    {
        $errors = array();
        $options = array(
                'title' =>array(
                        'required' => array(
                                'rule'=>true,
                                'message'=>L('_ERR_EMPTY_TITLE_')
                        ),
                        'string_count_in_range' => array(
                                'rule'=>array(2,100),
                                'message'=>L('_ERR_LENGTH_TITLE_', array('min'=>2, 'max'=>100))
                        ),
                ),
                'node_id' =>array(
                        'required' => array(
                                'rule'=>true,
                                'message'=>L('_ERR_EMPTY_CAT_')
                        ),
                ),
                'content' =>array(
                        'required' => array(
                                'rule'=>true,
                                'message'=>L('_ERR_EMPTY_CONTENT_')
                        ),
                ),
        );
        
        $rs = \Base\Lib\Tools\Validator::make($errors, $data, $options);
        if(!$rs)
        {
            foreach ($errors as $k=>$v)
            {
                throw new \Exception($v);
            }
        }
        
        return $rs;
    }
}
 